<?php
namespace backend\models\statistics;

use Yii;

class Statistics extends \yii\db\ActiveRecord
{
    /**
     * 数据统计 选择将要查询的类型数据
     * $id,$time,$table,$begin,$end,$type
     */
    public function getSearch($post = array()) {
        $begin = $post['begintime']?$post['begintime']:strtotime(date('Y-m-01',time()));
        $end = $post['endtime']?$post['endtime']:time();
        $type = $post['type']?$post['type']:'day';
        switch ($post['list']) {
            case 'newuser' : return $this->getInfo('user_id','user_regtime','edu_user',$begin,$end,$type); break;
            case 'newchapter' : return $this->getInfo('section_id','section_addtime','edu_course_section',$begin,$end,$type); break;
            case 'newsection' : return $this->getInfo('joint_id','joint_addtime','edu_course_joint',$begin,$end,$type); break;
            case 'newcourse' : return $this->getInfo('course_id','add_time','edu_course',$begin,$end,$type); break;
            case 'newclasshour' : return $this->getInfo('period_id','period_addtime','edu_course_period',$begin,$end,$type); break;
            case 'learning' : return $this->getInfo($post); break;
            case 'paycourse' : return $this->getInfo($post); break;
            case 'completeour' : return $this->getInfo($post); break;
            case 'video' : return $this->getInfo($post); break;
                    default: return $this->getInfo('user_id','user_regtime','edu_user',$begin,$end,$type);
        }
    }    
   
    /**
     * 数据统计 新增课程数
     */
    public function getInfo($id,$time,$table,$begin,$end,$type) {       
        $rows = (new \yii\db\Query())
        ->select([$id,$time])
        ->from($table)        
        ->where(['between', $time,$begin,$end])
        ->all();
        if(!empty($rows)){
            if($type=='day'){
                foreach ($rows as $key=>$val) {
                    $new[date('d',$val[$time])][] = $val[$id]; 
                }
                ksort($new);   
                $j=0;
                for($i=1;$i<=date('d',$end);$i++){
                    $i = ($i>1)&&($i<10)?'0'.$i:$i; 
                    if(count($new[$i])>0){
                        $arr[$j]['id'] = $j;
                        $arr[$j]['sales'] = count($new[$i]);
                        $arr[$j]['year'] = $i.'day';
                    }else{
                        $arr[$j]['id'] = $j;
                        $arr[$j]['sales'] = 0;
                        $arr[$j]['year'] = $i.'day';
                    }
                    $j++;
                }
            }else if($type=='month'){
                foreach ($rows as $key=>$val) {
                    $new[date('m',$val[$time])][] = $val[$id];
                }
                ksort($new);
                $j=0;
                for($i=date('m',$begin);$i<=date('m',$end);$i++){
                    $i = ($i>1)&&($i<10)?'0'.$i:$i;
                    if(count($new[$i])>0){
                        $arr[$j]['id'] = $j;
                        $arr[$j]['sales'] = count($new[$i]);
                        $arr[$j]['year'] = $i.'month';
                    }else{
                        $arr[$j]['id'] = $j;
                        $arr[$j]['sales'] = 0;
                        $arr[$j]['year'] = $i.'month';
                    }
                    $j++;
                }
            }else{
                return ['info'=>'请求格式错误','data'=>0];
            }
            return $arr; 
        }else{
            return ['info'=>'没有找到相关数据','data'=>0];
        }     
       
    }
 
}

?>